package com.ruoyi.system.service.impl;

import java.util.List;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.CommonConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysOssConfigMapper;
import com.ruoyi.system.domain.SysOssConfig;
import com.ruoyi.system.service.ISysOssConfigService;

import javax.annotation.PostConstruct;

/**
 * 存储配置Service业务层处理
 * 
 * @author zouhuu
 * @date 2022-07-24
 */
@Service
public class SysOssConfigServiceImpl implements ISysOssConfigService 
{
    @Autowired
    private SysOssConfigMapper sysOssConfigMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 初始化对象存储配置
     */
    @PostConstruct
    public void init() {
        List<SysOssConfig> sysOssConfigs = sysOssConfigMapper.selectSysOssConfigList(new SysOssConfig());
        sysOssConfigs.stream().forEach(item -> {
            if (item.getStatus() == CommonConstants.DATA_STATUS_NORMAL) {
                redisCache.setCacheObject(CacheConstants.CACHE_CONFIG_KEY, item.getConfigKey());
            }
            redisCache.setCacheObject(CacheConstants.SYS_OSS_KEY + item.getConfigKey(), JSONUtil.toJsonStr(item));
        });
    }

    /**
     * 查询存储配置
     * 
     * @param ossConfigId 存储配置主键
     * @return 存储配置
     */
    @Override
    public SysOssConfig selectSysOssConfigByOssConfigId(Long ossConfigId)
    {
        return sysOssConfigMapper.selectSysOssConfigByOssConfigId(ossConfigId);
    }

    /**
     * 查询存储配置列表
     * 
     * @param sysOssConfig 存储配置
     * @return 存储配置
     */
    @Override
    public List<SysOssConfig> selectSysOssConfigList(SysOssConfig sysOssConfig)
    {
        return sysOssConfigMapper.selectSysOssConfigList(sysOssConfig);
    }

    /**
     * 新增存储配置
     * 
     * @param sysOssConfig 存储配置
     * @return 结果
     */
    @Override
    public int insertSysOssConfig(SysOssConfig sysOssConfig)
    {
        sysOssConfig.setCreateTime(DateUtils.getNowDate());
        return sysOssConfigMapper.insertSysOssConfig(sysOssConfig);
    }

    /**
     * 修改存储配置
     * 
     * @param sysOssConfig 存储配置
     * @return 结果
     */
    @Override
    public int updateSysOssConfig(SysOssConfig sysOssConfig)
    {
        sysOssConfig.setUpdateTime(DateUtils.getNowDate());
        return sysOssConfigMapper.updateSysOssConfig(sysOssConfig);
    }

    /**
     * 批量删除存储配置
     * 
     * @param ossConfigIds 需要删除的存储配置主键
     * @return 结果
     */
    @Override
    public int deleteSysOssConfigByOssConfigIds(Long[] ossConfigIds)
    {
        return sysOssConfigMapper.deleteSysOssConfigByOssConfigIds(ossConfigIds);
    }

    /**
     * 删除存储配置信息
     * 
     * @param ossConfigId 存储配置主键
     * @return 结果
     */
    @Override
    public int deleteSysOssConfigByOssConfigId(Long ossConfigId)
    {
        return sysOssConfigMapper.deleteSysOssConfigByOssConfigId(ossConfigId);
    }
}
